package com.zby.business.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zby.business.R;

/**
 * Created by BY on 2016/1/29.
 */
public class PullToRefreshView extends LinearLayout implements View.OnTouchListener {
    private ImageView imageView;
    private TextView textView;
    private float lastY;
    private LayoutInflater inflater;

    public PullToRefreshView(Context context) {
        super(context);
    }

    public PullToRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.pull_to_refresh_header, null);
        textView = (TextView) view.findViewById(R.id.pullToRefreshTextView);
        imageView = (ImageView) view.findViewById(R.id.pullToRefreshImageView);
        addView(view);
        setPadding(0, -200, 0, 0);
        setOnTouchListener(this);

    }

    public PullToRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                System.out.println(getPaddingTop() + "");
                lastY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println(getPaddingTop() + "     "+event.getY());
                if(getPaddingTop()<-201){
                    setPadding(0,-200,0,0);
                }
                if (getPaddingTop() > -201) {
                    setPadding(0, (int) -(-getPaddingTop() - (event.getY() - lastY)), 0, 0);
                }
                if (getPaddingTop() > 0) {
                    textView.setText("松开刷新");
                    imageView.setRotation(180);
                }
                lastY = event.getY();
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }
}
